/*
	Flie:						WorldSelectionState.cpp
	Course:				SGP 1208
	Author:				Kori Po
	Purpose:			World Selection Menu
*/
#include "Game.h"
#include "WorldSelectionState.h"
#include "MainMenuState.h"
#include "GameplayState.h"

// Temporary Development Includes
#include "TempFont.h"
#include "../TinyXML/tinyxml.h"

CWorldSelectionState::CWorldSelectionState(void)
{
	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;

	m_nBackGroundID = -1;
	m_nOptionsBoxID = -1;

	m_nSelectedItem = 0;

	CGame* TheGame = CGame::GetInstance();

	for(int x = 0; x < WORLDMENUITEMS; x++)
	{
		m_MenuSelection[x] = false;

		RECT r = {TheGame->GetScreenWidth()/8,
			(TheGame->GetScreenHeight()/4) + (75 * x), 
			TheGame->GetScreenWidth()/8 + (225*2),
			(TheGame->GetScreenHeight()/4) + (75 * x) + 43 };

		m_MenuRect[x] = r;
	}

	m_szMenuItem[0] = "LEVEL: 1 WORLD: 1";
	m_szMenuItem[1] = "LEVEL: 2 WORLD: 1";
	m_szMenuItem[2] = "LEVEL: 1 WORLD: 2";
	m_szMenuItem[3] = "LEVEL: 2 WORLD: 2";
	m_szMenuItem[4] = "MAP CORE";
	m_szMenuItem[5] = "EXIT";
}


CWorldSelectionState::~CWorldSelectionState(void)
{
	/* Do Nothing */
}


/*static*/ CWorldSelectionState* CWorldSelectionState::GetInstance( void )
{
	// Static local variable - Stored in GLOBAL memory
	static CWorldSelectionState s_instance;
	return &s_instance;
}

void CWorldSelectionState::Enter(void)
{
	m_3DGuy					= CSGD_Direct3D::GetInstance();
	m_InputGuy			= CSGD_DirectInput::GetInstance();
	m_TextureGuy		= CSGD_TextureManager::GetInstance();
	m_AudioGuy			= CSGD_XAudio2::GetInstance();

	m_nMenuWizardID	= m_TextureGuy->LoadTexture(_T("Resource/TempMouseCursor.png"), D3DCOLOR_XRGB(255,255,255));
	m_nBackGroundID	= m_TextureGuy->LoadTexture(_T("Resource/TempFusionBackground.png"), D3DCOLOR_XRGB(255,255,255));
	m_nOptionsBoxID		= m_TextureGuy->LoadTexture(_T("Resource/TempOptionItemBox.png"), D3DCOLOR_XRGB(255,255,255));

	m_MenuFont				= new CTempFont( );
}

void CWorldSelectionState::Exit(void)
{
	if(m_nMenuWizardID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nMenuWizardID);
		m_nMenuWizardID = -1;
	}
	if(m_nOptionsBoxID !=1)
	{
		m_TextureGuy->UnloadTexture(m_nOptionsBoxID);
		m_nOptionsBoxID = -1;
	}

	m_3DGuy					= nullptr;
	m_InputGuy			= nullptr;
	m_TextureGuy		= nullptr;
	m_AudioGuy			= nullptr;
}

bool CWorldSelectionState::Input(void)
{
	if( m_InputGuy->KeyDown( DIK_LMENU ) && m_InputGuy->KeyPressed( DIK_RETURN )|| m_InputGuy->KeyDown( DIK_RMENU ) && m_InputGuy->KeyPressed( DIK_RETURN ) )
	{
			CGame* TheGame	=	CGame::GetInstance();
			TheGame->SetIsWindowed(!TheGame->GetIsWindowed());
			m_3DGuy->ChangeDisplayParam(TheGame->GetScreenWidth(),TheGame->GetScreenHeight(),TheGame->GetIsWindowed());
			return true;
	}

	if( m_InputGuy->KeyPressedEx(DIK_DOWN) || m_InputGuy->JoystickDPadPressed(DIR_DOWN, 0) )
	{
		if(m_nSelectedItem + 1 > 5)
		{
			m_nSelectedItem = 0;
		}
		else
		{
			m_nSelectedItem++;
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_UP) || m_InputGuy->JoystickDPadPressed(DIR_UP, 0) )
	{
		if(m_nSelectedItem - 1 < 0)
		{
			m_nSelectedItem = 5;
		}
		else
		{
			m_nSelectedItem--;
		}
	}

	if( m_InputGuy->KeyPressedEx(DIK_RETURN ) || m_InputGuy->MouseButtonPressed(0) || m_InputGuy->JoystickButtonPressed( 0, 0) )
	{
		if(m_nSelectedItem == 0)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();

			ThePlay->SetLevel(1);
			ThePlay->SetWorld(1);

			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 1)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();

			ThePlay->SetLevel(2);
			ThePlay->SetWorld(1);

			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 2)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();

			ThePlay->SetLevel(1);
			ThePlay->SetWorld(2);

			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 3)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CGameplayState* ThePlay = CGameplayState::GetInstance();

			ThePlay->SetLevel(2);
			ThePlay->SetWorld(2);

			TheGame->ChangeState(ThePlay);
			return true;
		}
		else if(m_nSelectedItem == 4)
		{
			return true;
		}
		else if(m_nSelectedItem == 5)
		{
			CGame* TheGame	=	CGame::GetInstance();
			CMainMenuState* TheMain = CMainMenuState::GetInstance();
			TheGame->ChangeState(TheMain);
			return true;
		}
	}
	
	for(int x = 0; x < 6; x++)
	{
		m_MenuSelection[x] = false;
	}

	if( m_InputGuy->KeyPressedEx(DIK_ESCAPE ) || m_InputGuy->JoystickButtonPressed( 2, 0)  )
	{
		m_nSelectedItem = 5;
		return true;
	}

	m_MenuSelection[m_nSelectedItem] = true;
	return true;
}

void CWorldSelectionState::Update(float fElapsedTime)
{
	Mouseinthebox( );
}

void CWorldSelectionState::Render(void) 
{
	CGame* TheGame = CGame::GetInstance();

	//Draw Background
	RECT ToDraw = {0,0,1920,1080};
	m_TextureGuy->Draw(m_nBackGroundID,0,0,1,1,&ToDraw);

	// Draw Menu
	for(int x = 0; x < WORLDMENUITEMS; x++)
	{
		m_TextureGuy->Draw(m_nOptionsBoxID,TheGame->GetScreenWidth()/8,(TheGame->GetScreenHeight()/4) + (75 * x),3,1);
		if(!m_MenuSelection[x])
		{
			m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/8 + 50,(TheGame->GetScreenHeight()/4) + (75 * x) + 15, 1, D3DCOLOR_XRGB(255,255,255));
		}
		else
		{
			m_MenuFont->Print(m_szMenuItem[x], TheGame->GetScreenWidth()/8 + 50,(TheGame->GetScreenHeight()/4) + (75 * x) + 15, 1.3, D3DCOLOR_XRGB(255,0,0));
		}
	}

	// Draw Title
	m_MenuFont->Print("PICK A WORLD", TheGame->GetScreenWidth()/2 - 50, 20, 1, D3DCOLOR_XRGB(255,255,255));

	// Draw Mouse
	RECT ToDrawMouse = {0,0,38,48};
	if(m_InputGuy->MouseButtonDown(MOUSE_LEFT))
	{
		RECT ToDrawMouse = {39,0,75,48};
	}

	m_TextureGuy->Draw(m_nMenuWizardID,m_InputGuy->MouseGetPosX(),m_InputGuy->MouseGetPosY(),1,1,&ToDrawMouse);

}

void CWorldSelectionState::Mouseinthebox()
{
	int MouseX = m_InputGuy->MouseGetPosX();
	int MouseY = m_InputGuy->MouseGetPosY();
	
	POINT Clicker;
	Clicker.x = m_InputGuy->MouseGetPosX();
	Clicker.y = m_InputGuy->MouseGetPosY();

	CGame* TheGame = CGame::GetInstance();
	
	for(int x = 0; x < WORLDMENUITEMS; x++)
	{
		if(PtInRect(&m_MenuRect[x], Clicker))
		{
			m_nSelectedItem = x;
		}
	}
}